//
// Created by spiro on 19-3-10.
//

#include <stdio.h>
#include <string.h>

char * s_gets(char * st, int n);
#define MAXTITL 41
#define MAXAUTL 31
#define MAXBKS 100

struct book {
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void) {
    struct book library[MAXBKS];

    printf("Press [enter] at the start of a line to stop.\n");

    int count = 0;
    while (count < MAXBKS) {
        printf("Please enter the%s book title.\n", (count == 0 ? "" : " next"));
        if (s_gets(library[count].title, MAXTITL) == NULL
                || library[count].title[0] == '\0') {
            break;
        }

        printf("Now enter the author.\n");
        s_gets(library[count].author, MAXAUTL);
        printf("Now enter the value.\n");
        scanf("%f", &library[count].value);

        while (getchar() != '\n')
            continue;

        count++;
    }

    if (count > 0) {
        printf("Here is the list of your books:\n");
        for (int i = 0; i < count; ++i) {
            printf("%s by %s: $%.2f\n", library[i].title, library[i].author, library[i].value);
        }
    } else {
        printf("No books!");
    }
    return 0;
}

char * s_gets(char * st, int n) {
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val) {
        if (find = strchr(st, '\n')) {
            *find = '\0';
        } else {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}